package com.gopay.scheduler.job;

import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.cbrmb.CbRmbIntlDishonorAccountingFailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author b.zou
 * @description: 拒付或调单订单，若扣款（冻结）失败，跑批也没有扣款（冻结）成功，则每三天发邮件通知商户，订单不重复通知
 * @class CbRmbIntlDishonorAccountingFailCustEmailJob
 * @date 2018.1.5
 */
public class CbRmbIntlDishonorAccountingFailCustEmailJob extends GopayBaseJob {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "cbRmbIntlDishonorAccountingFailService")
    private CbRmbIntlDishonorAccountingFailService cbRmbIntlDishonorAccountingFailService;

    @Override
    protected void doJobInternal() {
        logger.info("======= CbRmbIntlDishonorAccountingFailCustEmailJob - 跨境外卡拒付(调单)订单超过三天扣款失败发送商户邮件定时任务 START=======");
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -3);
            Date date = c.getTime();
            /* 三天前零点 */
            Date timeBegin = new Date(date.getYear(), date.getMonth(), date.getDate(), 0, 0, 0);
            /* 三天前末点 */
            Date timeEnd = new Date(date.getYear(), date.getMonth(), date.getDate(), 23, 59, 59);
            Map<String, Object> paramsOfCust = new HashMap<>(2);
            paramsOfCust.put("timeBegin", sdf.format(timeBegin));
            paramsOfCust.put("timeEnd", sdf.format(timeEnd));
            /* find custIds */
            List<String> list = cbRmbIntlDishonorAccountingFailService.getCustIds(paramsOfCust);
            if (null != list && list.size() > 0) {
                for (String custId : list) {
                    try {
                        /* find OrderIds by custId and sendEmail */
                        paramsOfCust.put("custId", custId);
                        cbRmbIntlDishonorAccountingFailService.sendCustEmail(paramsOfCust);
                    } catch (Exception e) {
                        logger.error("外卡拒付（调单）订单超过三天扣款失败发送商户邮件失败，商户ID：{}，失败原因：{}", new Object[]{custId, e});
                    }
                }
            }
        } catch (Exception e) {
            logger.error("外卡拒付（调单）订单超过三天扣款失败发送商户邮件失败，失败原因：{}", e);
        }
        logger.info("======= CbRmbIntlDishonorAccountingFailCustEmailJob - 跨境外卡拒付(调单)订单超过三天扣款失败发送商户邮件定时任务 STOP =======");
    }


}
